package io.jnet.server.serverchannelcontext;

import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.BlockingQueue;
import io.jnet.common.api.ReadHandler;
import io.jnet.common.api.impl.ConcentrationReadHandler;
import io.jnet.common.bufstorage.BufStorage;
import io.jnet.common.bufstorage.BufStorage.StorageType;
import io.jnet.common.decodec.FrameDecodec;
import io.jnet.common.streamprocessor.ProcessorTask;
import io.jnet.common.streamprocessor.StreamProcessor;
import io.jnet.common.util.AioListener;

public class ConcentrationServerChannelContext extends AbstractServerChannelContext
{
    private final ReadHandler readHandler;
    
    public ConcentrationServerChannelContext(//
            BlockingQueue<ProcessorTask> tasks, //
            BufStorage bufStorage, //
            int maxMerge, //
            AioListener serverListener, //
            StreamProcessor[] processors, //
            AsynchronousSocketChannel socketChannel, //
            FrameDecodec frameDecodec)
    {
        super(bufStorage, maxMerge, serverListener, processors, socketChannel, frameDecodec);
        if (bufStorage.type() == StorageType.spsc)
        {
            // 由于同一个通道的消息可能会被不同的线程执行，所以存在多个线程将buf写入bufstorage。因此只能使用MPSC类型的
            throw new UnsupportedOperationException("只支持MPSC类型的BufStorage");
        }
        readHandler = new ConcentrationReadHandler(tasks, socketChannel, frameDecodec, ioBuf, serverListener, this);
    }
    
    @Override
    public void registerRead()
    {
        readHandler.registerRead();
    }
    
}
